package com.zhnc.dangjianpingtaiapp.gui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.wangdh.utilslibrary.utils.logger.TLog;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.action.ActionUpdataInteface;
import com.zhnc.dangjianpingtaiapp.argum.ItemListRely;
import com.zhnc.dangjianpingtaiapp.gui.base.BaseTitleFragment;
import com.zhnc.dangjianpingtaiapp.utils.recycler.RecyclerCommonAdapter;
import com.zhnc.dangjianpingtaiapp.utils.recycler.RecyclerViewHolder;
import com.zhnc.dangjianpingtaiapp.utils.recycler.grid.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangdh
 * @time 2019/4/2 15:12
 * @describe
 */
public class ItemListFragment extends BaseTitleFragment implements ActionUpdataInteface {
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_item_grid, container, false);
//    }

    private RecyclerView rv_item_grid;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.fragment_item_grid);
        super.onActivityCreated(savedInstanceState);
        rv_item_grid = getView().findViewById(R.id.rv_item_grid);

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(GridLayoutManager.VERTICAL);
        rv_item_grid.setLayoutManager(manager);
        DividerItemDecoration vDividerItemDecoration = new DividerItemDecoration(this.getContext(), LinearLayoutManager.VERTICAL);
        rv_item_grid.addItemDecoration(vDividerItemDecoration);

//        final List<String> data = new ArrayList<>();
//        for (int i = 0; i < 60; i++) {
//            data.add("itme" + i);
//        }
//        RecyclerCommonAdapter<String> stringRecyclerCommonAdapter = new RecyclerCommonAdapter<String>(this.getActivity(), R.layout.item_list, data) {
//            @Override
//            public void updataUI(RecyclerViewHolder recyclerViewHolder, String s) {
////                recyclerViewHolder.setText(R.id.itme_test_text, "更新" + s);
//            }
//        };
////设置适配器
//        rv_item_grid.setAdapter(stringRecyclerCommonAdapter);

        if (this.rely != null) {
            this.rely.requestData(this);
            rv_item_grid.setAdapter(this.adapter);
        }
    }

    private ItemListRely rely;
    private RecyclerCommonAdapter adapter;

    public void setRely(ItemListRely rely) {
        setTitleRely(rely);
        this.rely = rely;
        this.adapter = this.rely.getAdapter();
    }

    private void test() {
        rv_item_grid = getView().findViewById(R.id.rv_item_grid);
        final List<String> data = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            data.add("itme" + i);
        }

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
//        manager.setAutoMeasureEnabled(true);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        rv_item_grid.setLayoutManager(manager);// 将SnapHelper attach 到RecyclrView
//        rec.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        int dimensionPixelSize = getResources().getDimensionPixelSize(R.dimen.dp65);
//        DividerGridViewItemDecoration dividerGridViewItemDecoration = new DividerGridViewItemDecoration(this.getActivity(),dimensionPixelSize,0,0,0);
//
//        rv_item_grid.addItemDecoration(dividerGridViewItemDecoration);

//        new ViewPageSnap().attachToRecyclerView(rv_item_grid);
        DividerItemDecoration vDividerItemDecoration = new DividerItemDecoration(this.getContext(), LinearLayoutManager.VERTICAL);
        rv_item_grid.addItemDecoration(vDividerItemDecoration);
        RecyclerCommonAdapter<String> stringRecyclerCommonAdapter = new RecyclerCommonAdapter<String>(this.getActivity(), R.layout.item_list, data) {
            @Override
            public void updataUI(RecyclerViewHolder recyclerViewHolder, String s) {
//                recyclerViewHolder.setText(R.id.itme_test_text, "更新" + s);
            }
        };
//设置适配器
        rv_item_grid.setAdapter(stringRecyclerCommonAdapter);
    }

    @Override
    public void updata(List msg) {
        TLog.e("更新了" + msg);
        this.adapter.setmDatas(msg);
    }


}
